import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import Layout from '@/components/layout'
import cookie from '@/utils/cookie'

Vue.use(Router)

const routes = [{
  path: '*',
  component: () =>
    import('../../src/view/extension/404')
}, {
  path: '/',
  component: () =>
    import('../../src/view/index'),
  async beforeEnter (to, from, next) {
    if (cookie.get('device_id') && !sessionStorage.getItem('token')) {
      sessionStorage.setItem('token', cookie.get('device_id'))
    }
    if (sessionStorage.getItem('token')) {
      await store.dispatch('getUserInfo')
      next('/dashboard')
    }
    next()
  }
}, {
  path: '/index',
  meta: {
    auth: false,
    title: '登录'
  },
  component: () =>
    import('../../src/view/index')
}, {
  path: '/register',
  meta: {
    auth: false,
    title: '注册'
  },
  component: () =>
    import('../../src/view/register')
}, {
  path: '/protocol',
  meta: {
    auth: false,
    title: '注册协议'
  },
  component: () =>
    import('../../src/view/protocol')
}, {
  path: '/activateEmail',
  meta: {
    auth: false,
    title: '激活邮箱'
  },
  component: () =>
    import('../../src/view/register/activateEmail')
}, {
  path: '/registerByEmailSuccess',
  meta: {
    auth: false,
    title: '激活邮箱成功'
  },
  component: () =>
    import('../../src/view/register/registerByEmailSuccess')
}, {
  path: '/registerByPhoneSuccess',
  meta: {
    auth: false,
    title: '手机注册成功'
  },
  component: () =>
    import('../../src/view/register/registerByPhoneSuccess')
}, {
  path: '/passwordFind',
  meta: {
    auth: false,
    title: '找回密码'
  },
  component: () =>
    import('../../src/view/password-find')
}, {
  path: '/dashboard',
  component: Layout,
  redirect: '/dashboard/index',
  children: [{
    path: '/dashboard/index',
    component: () =>
      import('../../src/view/dashboard'),
    name: 'home/dashboard',
    meta: {
      auth: true,
      title: '我的主页'
    }
  }]
}, {
  path: '/myStar',
  component: Layout,
  redirect: '/myStar/index',
  children: [{
    path: '/myStar/index',
    component: () =>
      import('../../src/view/my-star'),
    name: 'home/myStar',
    meta: {
      auth: true,
      title: '我的收藏'
    }
  }]
}, {
  path: '/notice',
  component: Layout,
  redirect: '/notice/index',
  children: [{
    path: '/notice/index',
    component: () =>
      import('../../src/view/notice'),
    name: 'home/notice',
    meta: {
      auth: true,
      title: '用户中心-消息'
    }
  }]
}, {
  path: '/noticeDetail',
  component: Layout,
  redirect: '/noticeDetail/index',
  children: [{
    path: '/noticeDetail/index',
    component: () =>
      import('../../src/view/notice/detail'),
    name: 'home/noticeDetail',
    meta: {
      auth: true,
      title: '用户中心-消息详情'
    }
  }]
}, {
  path: '/noticeSend',
  component: Layout,
  redirect: '/noticeSend/index',
  children: [{
    path: '/noticeSend/index',
    component: () =>
      import('../../src/view/notice/send'),
    name: 'home/noticeSend',
    meta: {
      auth: true,
      title: '用户中心-消息回复'
    }
  }]
}, {
  path: '/account',
  component: Layout,
  redirect: '/account/index',
  children: [{
    path: '/account/index',
    component: () =>
      import('../../src/view/account'),
    name: 'home/account',
    meta: {
      auth: true,
      title: '个人账户'
    }
  }]
}, {
  path: '/siteCenter',
  component: Layout,
  redirect: '/siteCenter/index',
  children: [{
    path: '/siteCenter/index',
    component: () =>
      import('../../src/view/site-center'),
    name: 'siteCenter',
    meta: {
      auth: true,
      title: '用户中心-开放中心'
    }
  }]
}, {
  path: '/authentication',
  component: Layout,
  redirect: '/authentication/index',
  children: [{
    path: '/authentication/index',
    component: () =>
      import('../../src/view/authentication'),
    name: 'authentication',
    meta: {
      auth: true,
      title: '用户中心-认证中心'
    }
  }]
}]

const router = new Router({
  // mode: 'history',
  routes,
  scrollBehavior (to, from, savedPosition) {
    return savedPosition || {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach(async (to, from, next) => {
  // 不需要登录
  if (!to.matched.some(record => record.meta.auth)) {
    next()
    return
  }

  // 弹出窗口登陆后， 每个路由前先获取用户信息
  if (cookie.get('device_id')) {
    sessionStorage.setItem('token', cookie.get('device_id'))
  } else {
    sessionStorage.removeItem('token')
  }

  let token = sessionStorage.getItem('token')
  if (token) {
    await store.dispatch('getUserInfo')
    // 登录后强制刷新页面，用户信息从sessionStorage获取
    let me = sessionStorage.getItem('me')
    // let jsonMe = JSON.parse(me)
    // console.log(jsonMe)
    if (me) {
      store.commit('GET_USER_INFO', JSON.parse(me))
      next()
    } else {
      let res = await store.dispatch('getUserInfo')
      if (res.user.userAccount) {
        next()
      }
    }
  } else {
    next('/index')
  }

  // 未登录，本地也无token，跳转到登录页，同时记录来源页，登录后可跳转到相应页面
  // next(`/?redirectPath=${to.fullPath}`)
})

export default router
